package cn.itcast.item.service.servicelimp;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.order.OrderLogistics;
import cn.itcast.item.constants.MqConstants;
import cn.itcast.item.mapper.itenMapper;
import cn.itcast.item.service.serviceitem;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ServiceItemLimp extends ServiceImpl<itenMapper, Item> implements serviceitem {
    @Autowired
    itenMapper itenMapper;


    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public PageDTO<Item> selectmitemlist(SearchItemDTO searchItemDTO) {

        Page<Item> page = new Page(searchItemDTO.getPage(), searchItemDTO.getSize());
        if (StringUtils.isBlank(searchItemDTO.getName()) && StringUtils.isBlank(searchItemDTO.getBrand()) && StringUtils.isBlank(searchItemDTO.getCategory())) {
            itenMapper.selectPage(page, null);
        } else {

            LambdaQueryWrapper<Item> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
            objectLambdaQueryWrapper.eq(Item::getName, searchItemDTO.getName())
                    .or().between(Item::getCreateTime, searchItemDTO.getBeginDate(), searchItemDTO.getBeginDate())
                    .or().eq(Item::getBrand, searchItemDTO.getBrand())
                    .or().eq(Item::getCategory, searchItemDTO.getCategory());

            itenMapper.selectPage(page, objectLambdaQueryWrapper);

        }
        List<Item> records = page.getRecords();
        long total = page.getTotal();
        return new PageDTO<>(total, records);
    }

    @Override
    public Item selecitem(Long id) {
        Item item = itenMapper.selectById(id);
        return item;
    }



    @Override
    public ResultDTO addstatus(Long id, Integer status) {
        Item selecitem = selecitem(id);
        ResultDTO resultDTO = new ResultDTO();
        if (selecitem != null) {
            Item item = new Item();
            item.setId(id);
            item.setStatus(status);
            //修改商品状态
            int i = itenMapper.updateById(item);
            //上架发送1下架发送2给es发送mq消息
            if (status == 1) {
                rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE, MqConstants.HMALL_INSERT_KEY, id);
                System.out.println("发送上架成功");
            } else if (status == 2) {
                rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE, MqConstants.HMALL_DELETE_KEY, id);
                System.out.println("发送删除成功");

            }
            Item item1 = itenMapper.selectById(id);

            if (i >= 0) {
                resultDTO.setSuccess(true);
                resultDTO.setMsg("操作成功");
                resultDTO.setData(item1);
                return resultDTO;
            }
        }
        return resultDTO.error();
    }

    //修改Ietm
    @Override
    public ResultDTO updateitem(Item item) {
        int i = itenMapper.updateById(item);
        return ResultDTO.ok();
    }
   //删除item
    @Override
    public ResultDTO DelectItem(Long id) {
        boolean b = removeById(id);
        if (b) {
            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }

    @Override
    public List<Item> listitems() {
        Page<Item> itemPage = new Page<>();
        LambdaQueryWrapper<Item> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<Item> itemPage1 = itenMapper.selectPage(itemPage, objectLambdaQueryWrapper);
        List<Item> records = itemPage.getRecords();
        System.out.println(records);
        return records;
    }

    @Override
    public Item additem(String id) {
        long l = Long.parseLong(id);
        Item selecitem = selecitem(l);
        return selecitem;
    }



}
